package com.link.gateway.base.visitfrequency.service;

import com.link.gateway.core.filter.visitstatistics.util.VisitConfigSingleton;
import com.link.gateway.core.filter.visitstatistics.util.VisitStatisticsConstants;
import com.link.gateway.core.redis.redisclient.LinkRedisClient;
import com.link.gateway.core.utils.RedisUtil;
import com.link.gateway.core.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 接口访问统计service，主要用于加载接口访问配置
 *
 * @author LiYang
 * @date 20190826
 */
@Service
public class VisitFrequencyServiceImpl implements VisitFrequencyService {

    @Override
    public boolean loadConfig() {
        boolean result = true;
        LinkRedisClient jedis = null;
        try {
            jedis = RedisUtil.getJedis();
            VisitConfigSingleton instance = VisitConfigSingleton.getInstance();
            String key = VisitStatisticsConstants.API_STATISTICS_CONFIG_KEY;
            //功能状态
            instance.setStatus(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_STATUS));
            //频率时间范围
            instance.setLimitTime(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_LIMIT_TIME));
            //单位时间预警请求数量
            instance.setLimitVisitNum(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_LIMIT_VISIT_NUM));
            //单位时间越权请求数量
            instance.setLimitAuthNum(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_LIMIT_AUTH_NUM));
            //是否发送短信预警
            instance.setMsgAlert(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_MSG_ALERT));
            //是否发送邮件预警
            instance.setEmailAlert(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_EMAIL_ALERT));
            //短信模板编码
            instance.setMsgExampleCode(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_MSG_EXAMPLE_CODE));
            //短信参数
            instance.setMsgParams(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_MSG_PARAMS));
            //短信类型
            instance.setMsgType(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_MSG_TYPE));
            //短信通知手机号
            instance.setMsgPhones(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_MSG_PHONES));
            //警报通知时间间隔
            instance.setAlertTimeInterval(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_ALERT_TIME_INTERVAL));
            //通知邮箱
            instance.setEmailAddrs(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_EMAIL_ADDRS));
            //是否启用超频限制
            instance.setAccessRestriction(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_ACCESS_RESTRICTION));
            //不记录接口
            String apiConfigNoRecord = jedis.hget(key, VisitStatisticsConstants.API_CONFIG_NO_RECORD);
            if (!StringUtils.isBlank(apiConfigNoRecord)) {
                String[] configs = apiConfigNoRecord.split(",");
                Map<String, Boolean> configMap = new HashMap<>(16);
                for (int i = 0; i < configs.length; i++) {
                    configMap.put(configs[i], true);
                }
                instance.setNoRecord(configMap);
            }
//            instance.setNoRecord(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_NO_RECORD));
            //特殊接口
            String apiConfigSpecialUrl = jedis.hget(key, VisitStatisticsConstants.API_CONFIG_SPECIAL_URL);
            if (!StringUtils.isBlank(apiConfigSpecialUrl)) {
                String[] urlConfigs = apiConfigSpecialUrl.split(",");
                Map<String, Boolean> urlConfigMap = new HashMap<>(16);
                for (int i = 0; i < urlConfigs.length; i++) {
                    urlConfigMap.put(urlConfigs[i], true);
                }
                instance.setSpecialUrl(urlConfigMap);
            }
//            instance.setSpecialUrl(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_SPECIAL_URL));
            //特殊接口限制时间
            instance.setSpecialLimitTime(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_SPECIAL_LIMIT_TIME));
            //特殊接口限制次数
            instance.setSpecialLimitNum(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_SPECIAL_LIMIT_NUM));
            //特殊接口越权次数
            instance.setSpecialAuthNum(jedis.hget(key, VisitStatisticsConstants.API_CONFIG_SPECIAL_AUTH_NUM));
            //登录接口访问限制次数
            instance.setLoginLimitNum(jedis.hget(VisitStatisticsConstants.API_LOGIN_CONFIG, VisitStatisticsConstants.API_LOGIN_LIMIT_NUM));
            //登录接口访问限制单位时间
            instance.setLoginLimitTime(jedis.hget(VisitStatisticsConstants.API_LOGIN_CONFIG, VisitStatisticsConstants.API_LOGIN_LIMIT_TIME));
            //登录接口访问超频报警时间间隔
            instance.setLoginAlertInterval(jedis.hget(VisitStatisticsConstants.API_LOGIN_CONFIG,
                    VisitStatisticsConstants.API_LOGIN_ALERT_TIME_INTERVAL));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            result = false;
        } finally {
            RedisUtil.returnResource(jedis);
        }
        return result;
    }
}
